package org.jie.guider.framework.hikari.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.jie.guider.framework.hikari.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 * @author LiJie
 */
@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<User> findAll() {
        return jdbcTemplate.query("SELECT * from user", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getLong("id"));
                user.setName(rs.getString("name"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        });
    }

    public int insert(User user) {
        Object[] values = new Object[2];
        values[0] = user.getName();
        values[1] = user.getAge();
        return jdbcTemplate.update("insert into user(name, age) values(?, ?)", values);
    }
}
